//
//  NSDate+Addition.m
//  Runone
//
//  Created by runone02 on 16/9/28.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "NSDate+Addition.h"

@implementation NSDate (Addition)
- (NSString *)stringFromDate:(NSDate *)date dateFormat:(NSString *)dateFromat {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    // yyyy-MM-dd HH:mm:ss zzz
    [dateFormatter setDateFormat:dateFromat];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}

- (BOOL)isYesterday {

    NSDate * now = [NSDate date];
    NSDate *date = self;
    NSDateFormatter * format = [[NSDateFormatter alloc]init];
    format.dateFormat = @"yyyy-MM-dd";
    NSString *createdDate = [format stringFromDate:self];
    NSString *nowDate = [format stringFromDate:now];
    date = [format dateFromString:createdDate];
    now = [format dateFromString:nowDate];
    NSCalendar * celendar = [NSCalendar currentCalendar];
    NSDateComponents * Components = [celendar components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:now toDate:date options:0];
    return Components.month == 0 && Components.day == 1 && Components.year == 0;
}

- (BOOL)isToday {
    
    NSDate *now = [NSDate date];
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    format.dateFormat = @"yyyy-MM-dd";
    NSString *nowDate = [format stringFromDate:now];
    NSString *createDate = [format stringFromDate:self];
    
    return [nowDate isEqualToString:createDate];
    
}

- (BOOL)isThisYear {
    
    //创建日历进行比对
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    //获取当前数据年
    NSDateComponents *created =[calendar components:NSCalendarUnitYear fromDate:self];
    
    NSDateComponents *now =[calendar components:NSCalendarUnitYear fromDate:[NSDate date]];
    
    return created.year == now.year;
}

+ (NSDateComponents *)componentsFrom:(NSString *)dateString {
    //创建一个时间格式转换器
    NSDateFormatter *formatterDate=[[NSDateFormatter alloc] init];
    formatterDate.dateFormat=@"yyyy-MM-dd HH:mm:ss";
    
    NSTimeZone *zone   = [NSTimeZone systemTimeZone];
    NSDate *fromDate   = [formatterDate dateFromString:dateString];
    NSInteger occInter = [zone secondsFromGMTForDate: fromDate];
    NSDate *occurData  = [fromDate dateByAddingTimeInterval:occInter];
    
    NSDate *date       = [NSDate date];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    //创建一个日历牌对象
    NSCalendar *calender=[NSCalendar currentCalendar];
    //枚举保存日期的每一天
    NSCalendarUnit unitsave=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    
    //计算日期
    NSDateComponents *comTogether=[calender components:unitsave fromDate:occurData toDate:localeDate options:0];
    NSLog(@"开始时间与当前时间差为：%ld-%ld-%ld-%ld-%ld-%ld",(long)comTogether.year,(long)comTogether.month,(long)comTogether.day,(long)comTogether.hour,(long)comTogether.minute,(long)comTogether.second);
    return comTogether;
}

+ (NSUInteger )timeIntevalSinceRightNow:(NSString *)dateString {
    NSDateFormatter *formatterDate=[[NSDateFormatter alloc] init];
    formatterDate.dateFormat=@"yyyy-MM-dd HH:mm:ss";
    
//    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
//    [formatterDate setTimeZone:timeZone]; //设置时区 ＋8:00
    NSDate *date       = [NSDate date];
    NSTimeZone *zone   = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    NSDate *fromDate   = [formatterDate dateFromString:dateString];
    
    NSTimeInterval nowTime  = [localeDate timeIntervalSince1970];
    NSTimeInterval fromTime = [fromDate timeIntervalSince1970];
    
    NSUInteger nTime = [[NSNumber numberWithDouble:nowTime] integerValue];
    NSUInteger fTime = [[NSNumber numberWithDouble:fromTime] integerValue];
    
    NSInteger inter  = nTime - fTime;
    NSLog(@"%ld",(long)inter);
    return inter;
}

+(int)compareOneDay:(NSString *)oneDay withAnotherDay:(NSString *)anotherDay {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat      = @"yyyy-MM-dd HH:mm:ss";
//    NSString *oneDayStr            = [dateFormatter stringFromDate:oneDay];
//    NSString *anotherDayStr        = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDay];
    NSDate *dateB = [dateFormatter dateFromString:anotherDay];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result ==NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
    
}

/*********
 比较两个日期之间某年或某月或某日或某时等的具体差值
 *******/
+ (NSDateComponents *)differencewithDate:(NSString*)dateString withDate:(NSString*)anotherdateString{
    
        NSDateFormatter *formatterDate=[[NSDateFormatter alloc] init];
        formatterDate.dateFormat=@"yyyy-MM-dd HH:mm:ss";
        
        NSTimeZone *zone   = [NSTimeZone systemTimeZone];
        NSDate *fromDate   = [formatterDate dateFromString:dateString];
        NSInteger occInter = [zone secondsFromGMTForDate: fromDate];
        NSDate *occurData  = [fromDate dateByAddingTimeInterval:occInter];
        
        NSDate *date       = [NSDate date];
        NSInteger interval = [zone secondsFromGMTForDate: date];
        NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
        //创建一个日历牌对象
        NSCalendar *calender=[NSCalendar currentCalendar];
        //枚举保存日期的每一天
        NSCalendarUnit unitsave=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
        
        //计算日期
        NSDateComponents *comTogether=[calender components:unitsave fromDate:occurData toDate:localeDate options:0];
        NSLog(@"开始时间与当前时间差为：%ld-%ld-%ld-%ld-%ld-%ld",(long)comTogether.year,(long)comTogether.month,(long)comTogether.day,(long)comTogether.hour,(long)comTogether.minute,(long)comTogether.second);
        return comTogether;
}







@end
